#!/usr/bin/perl
#
# this-month - write how many have registered in the Linux Counter
# so far this month, and in 2 previous month.
# Pretty silly early in the month.
#
use Reportsub;
use POSIX;

$nowdate = strftime("%Y-%m-%d", gmtime(time()));
warn "Nowdate is $nowdate\n";

while (<>) {
    ($date, $country, $sum) = split(":");
    # save the last data for each country under "nowdate"
    $lastdate = $date;
    $sums{$nowdate}{$country} = $sum;
    # keep only the first-of-month data
    $date = substr($date, 0, 7);
    next if $sums{$date}{$country};
    $is{$country} = 1;
    $sums{$date}{$country} = $sum;
}

# Find the last 3 dates
@dates = sort {$b cmp $a} keys(%sums);

# Skip countries that did not have a record for the next-to-last date
# (last date will always be set - this is a kind of bug)

for $country (keys(%is)) {
    if (!defined($sums{$dates[1]}{$country})) {
	# warn "Suppressing $country - empty in $dates[1]\n";
	delete $is{$country};
    }
}

for $datix (0..2) {
    for $country (keys(%is)) {
	$change = $sums{$dates[$datix]}{$country}
	-  $sums{$dates[$datix+1]}{$country};
	$change{$country}[$datix] = $change if $change != 0;
    }
    $showdate[$datix] = $dates[$datix+1];
}
$showdate[0] = substr($dates[0], 0, 7);

getcountryinfo();
print <<EoF;
<?php
 include "counter.php";
?>
<?php pagehead("Recent months' registration rates, per country") ?>
<BODY bgcolor="white">
<?php pagetop("Recent registration rates, per country") ?>
This list shows how many people registered with the Linux Counter
so far this month, and in the two previous months.
<p>
This list is sorted by registrations so far this month, and unlike
the <a href="short.php">standard report</a>, it is not weighted by
country size.
<p>
This may be a quite weird ranking near the beginning of the month, but
is likely to end up with the US on top as the month goes by.
<p>
This table includes registrations up to $dates[0].
<p>
<table border>
<tr><th>Rank<th><th>Country
EoF
for $datix (0..2) {
    print "<th>", $showdate[$datix];
    print "<th>Rank" if $datix > 0;
}
print "\n";

for $n (1..2) {
    $ix = new RankInc;
    for $country (sort({$change{$b}[$n] <=> $change{$a}[$n]} keys(%change))) {
       $ix->incr($change{$country}[$n]) if $country ne "All";
       $rank{$country}[$n] = $ix->show;
    }
}
$rank = new RankInc;
for $country (sort {$change{$b}[0] <=> $change{$a}[0]} keys(%change)) {
    $rank->incr($change{$country}[0]) if $country ne "All";
    print "<tr><td>", $rank->show();
    print "<td>", $country,"<td>",
    "<a href=\"/reports/place.php?place=$country\">$longname{$country}</a>";
    for $datix (0..2) {
	printf "<td>%d", $change{$country}[$datix];
        if ($datix > 0) {
            printf "<td>(%d)", $rank{$country}[$datix];
        }
    }
    print "\n";
}

print <<EoF;
</table>

<?php pagebottom() ?>
</body>
</html>
EoF

#------- Fun stuff: An incrementor for ranking lists ---------
package RankInc;

sub new {
    my $class = shift;
    my $first = shift;
    my $self = {};
    if (defined($first)) {
	$self->{value} = $first;
	$self->{count} = 1;
	$self->{show} = 1;
    } else {
	$self->{count} = 0;
	$self->{show} = 0;
    }
    bless $self;
    $self;
}

sub incr {
    my $self = shift;
    my $val = shift;
    ++ $self->{count};
    if ($val ne $self->{value}) {
	$self->{show} = $self->{count};
	$self->{value} = $val;
    }
}

sub show {
    my $self = shift;
    $self->{show};
}
